package com.unnamed.collisions.editor;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.Timer;


import com.unnamed.collisions.geometry.Polygon;
import com.unnamed.collisions.logging.Log;

public class PolyPanel extends JPanel{
	public Timer timer;
	public MouseHandler mouse;	
	
	public PolygonViewer viewer;
	public BufferedImage image;
	
	public PolyPanel(){
		super();

		viewer = new PolygonViewer();
		
		timer = new Timer(60, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				repaint();
			}
		});
		
		mouse = new MouseHandler(new SelectAction(viewer));
		
		this.addMouseListener(mouse);
		this.addMouseMotionListener(mouse);
		this.addMouseWheelListener(mouse);
		
		image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
		this.addComponentListener(new ComponentListener() {
			
			@Override
			public void componentShown(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println("Yaja");
			}
			
			@Override
			public void componentResized(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentMoved(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentHidden(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	
	}
	
	public void resize(){
		viewer.resize(getWidth(), getHeight());
		image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
		
	}
	
	public void paint(Graphics g){
		Graphics2D gd2 = (Graphics2D)g;
		viewer.draw((Graphics2D) image.getGraphics());
		gd2.drawImage(image, 0, 0,null);
	}

	/*
	 * Aux methods
	 */
	
//	public void paintComponent(Graphics g){
//		//((Graphics2D)g).drawImage(viewer.getImage(), 0, 0,null);
//	}
	
	
	/*
	 * Service methods
	 */
	
	public void startTimer(){
		Log.log("PolyPanel", "Starting timer");
		timer.start();
	}
	
	public void stopTimer(){
		Log.log("PolyPanel", "Stopping timer");
		timer.stop();
	}
}
